#include "stock20.h"
#include<iostream>
stock20::stock20()
{
    company = "no name";
    share = 0;
    share_val = 0.0;
    total_val = 0.0;
}
stock20::stock20(const std::string &co,long n,double pr)
{
    company = co;
    if(n<0)
    {
        std::cout<<"Number of share can't be negative; "
                <<company<<" shares set to 0.\n";
    }
    else
        share = n;
    share_val = pr;
    set_tot();
}
stock20::~stock20()
{
    std::cout<<"Destroy object;\n";
}
void stock20::buy(long num,double price)
{
    if(num<0)
    {
        std::cout <<"Number of share purcharsed can't be negative."
                 <<"Transcation is aborded.\n";
    }
    else{
        share += num;
        share_val = price;
        set_tot();
    }
}
void stock20::sell(long num,double price)
{
    using std::cout;
    if(num<0)
    {
        cout<<"Number of shares sold can't be negative."
           <<"Transcation is aborted.\n";
    }
    else if(num>share)
    {
        cout<<"You can't sell more than your have:"
           <<"Transcation is aborted.\n";
    }
    else{
        share -= num;
        share_val = price;
        set_tot();
    }
}
void stock20::update(double price)
{
    share_val = price;
    set_tot();
}
void stock20::show() const
{
    using std::cout;
    using std::ios_base;
    ios_base::fmtflags orig =
            cout.setf(ios_base::fixed,ios_base::floatfield);
    std::streamsize prec = cout.precision(3);
    cout<<"Company:"<<company<<" shares:"<<share<<share_val;
    cout.precision(2);
    cout<<"Total Worth:$"<<total_val<<'\n';
    cout.setf(orig,ios_base::floatfield);
    cout.precision(prec);
}
//这个函数用来对比两个stock20对象的总的total_val,参数为Stock20 返回的也是stock20,都是const
const stock20 &stock20::topval(const stock20 &s) const
{
    if(s.total_val>total_val)
        return s;
    else
        return *this;
}
